package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.dto.RoleDto;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.mapper.SysRoleMapper;
import com.itheima.ydd.service.ISysRoleMenuService;
import com.itheima.ydd.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private ISysRoleMenuService iSysRoleMenuService;
    @Override
    public void roleWithMenu(RoleDto roleDto) {
        this.updateById(roleDto);

        //先根据roleId删除
        LambdaQueryWrapper<SysRoleMenu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SysRoleMenu::getRoleId,roleDto.getId());
        iSysRoleMenuService.remove(lqw);
        //按集合存储
        List<SysRoleMenu> roleMenus=new LinkedList<>();

        for (Integer menuid : roleDto.getMenuids()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleDto.getId());
            sysRoleMenu.setMenuId(menuid.toString());
            roleMenus.add(sysRoleMenu);

        }
        iSysRoleMenuService.saveBatch(roleMenus);
    }
}
